package com.k.activemq.openwire3;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class Pub {
    public static void main(String[] args) {
        System.out.println("mq main start");
//        String url = "failover:(tcp://primary:61616,tcp://secondary:61616)?randomize=false";
        String url = "tcp://192.168.0.169:22000";
        url = "tcp://localhost:61616";
        String username = "admin";
        String password = "admin";//""Command_123";
        String topic = "ljw-topic";//""topic_test_1103";
        String topicReplay = "ljw-topic";//""topic_test_1103_replayTo";
        boolean pubSubDomain = true;
        ActiveMQConnectionFactory connectionFactory = null;
        Connection connection = null;
        Session session = null;
        MessageProducer producer = null;
        try {
            // ConnectionFactory
            connectionFactory = new ActiveMQConnectionFactory(url);
            // Connection
            connection = connectionFactory.createConnection(username, password);
            connection.start();
            // Session
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            // 目的地
            Destination destination = pubSubDomain ? session.createTopic(topic) : session.createQueue(topic);

            // 发送者
            producer = session.createProducer(destination);
            // 发送者 要发送的消息
            Message message11 = PubSubUtil.createMessage(session, "haha11", true);
            Message message22 = PubSubUtil.createMessage(session, "haha22", true);
            Message message33 = PubSubUtil.createMessage(session, "haha33", true);
            // 发送者-发送消息
            producer.send(message11);
            producer.send(message22);
            producer.send(message33);
            producer.send( PubSubUtil.createMessage(session, "haha44"));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            PubSubUtil.close(producer, null, session, connection);
        }
        System.out.println("mq main finish");
    }
}
